package com.example.community.dao;

import com.example.community.mapper.UsersMapper;
import com.example.community.model.Parking;
import com.example.community.model.Users;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

/**
 * 社区用户表
 */
public class UsersDao {
    public Users fandById(Integer id){
        try (SqlSession session = MybatisUtil.getSession()){
            final UsersMapper mapper = session.getMapper(UsersMapper.class);
            return mapper.selectByPrimaryKey(id);
        }
    }

    public List<Users> fandByAll(){
        try (SqlSession session = MybatisUtil.getSession()){
            final UsersMapper mapper = session.getMapper(UsersMapper.class);
            return mapper.selectAll();
        }
    }

    /**
     *  分页查询
     * @param ys 当前页数
     * @param shuju 每页显示的数据个数
     * @return List<Users>
     */
    public List<Users> fandLimit(Integer ys,Integer shuju){
        try (SqlSession session = MybatisUtil.getSession()){
            final UsersMapper mapper = session.getMapper(UsersMapper.class);
            Integer yss =(ys - 1) * shuju;
            return mapper.selectLimit(yss,shuju);
        }
    }

    public Integer selectLimitCount(Integer pageSize){
        try (SqlSession session = MybatisUtil.getSession()){
            final UsersMapper mapper = session.getMapper(UsersMapper.class);
            int count = mapper.CountUsers();
            return (count + pageSize - 1)/pageSize;
        }
    }


    public Users fandByAccount(String account){
        try (SqlSession session = MybatisUtil.getSession()){
            final UsersMapper mapper = session.getMapper(UsersMapper.class);
            return mapper.selectByAccount(account);
        }
    }

    public Integer insert(Users stu){
        try (SqlSession session = MybatisUtil.getSession()){
            final UsersMapper mapper = session.getMapper(UsersMapper.class);
            int i = mapper.insert(stu);
            session.commit();
            return i;
        }
    }

    public Integer delete(Integer id){
        try (SqlSession session = MybatisUtil.getSession()){
            final UsersMapper mapper = session.getMapper(UsersMapper.class);
            int i = mapper.deleteByPrimaryKey(id);
            session.commit();
            return i;
        }
    }

    public Integer update(Users users){
        try (SqlSession session = MybatisUtil.getSession()){
            final UsersMapper mapper = session.getMapper(UsersMapper.class);
            int i = mapper.updateByPrimaryKey(users);
            session.commit();
            return i;
        }
    }
}
